<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    let arr = [
      {
        menuName: '品牌活动',
        menuCode: 'activity',
        permissionName: 'activity:write'
      },
      {
        menuName: '品牌活动',
        menuCode: 'activity',
        permissionName: 'activity:red'
      },
      {
        menuName: '权限管理',
        menuCode: 'authority',
        permissionName: 'activity:write'
      },
      {
        menuName: '权限管理',
        menuCode: 'authority',
        permissionName: 'activity:red'
      }
    ];
    var mergeArr = () => {
      let hash = {};
      let arrItems = [];
      for (let i = 0; i < arr.length; i++) {
        let item = arr[i];
        let itemName = `${item.menuName}_${item.menuCode}`;
        if (!hash[itemName]) {
          hash[itemName] = item;
          arrItems.push(item);
        } else {
          for (var j = 0; j < arrItems.length; j++) {
            let comm = arrItems[j];
            if (
              comm.menuName == item.menuName &&
              comm.menuCode == item.menuCode
            ) {
              comm.permissionName += `,${item.permissionName}`;
              break;
            }
          }
        }
      }
      return arrItems;
    };
    console.log(mergeArr());
  </script>
</html>
